/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bombercloud.client;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;

/**
 *
 * @author Leonardo Oliveira Moreira
 */
public class BomberCloudMain extends Thread {

    private String serverAddress;
    private int serverPort;
    private String experimentFile;

    public BomberCloudMain(String serverAddress, int serverPort, String experimentFile) {
        this.serverAddress = serverAddress;
        this.serverPort = serverPort;
        this.experimentFile = experimentFile;
    }

    @Override
    public void run() {
        int executionTime = 5;
        long experimentTime = ((1000 * 60) * executionTime);
        int numberOfPlayers = 5;
        int avatarWidth = 50;
        int avatarHeight = 50;
        int dimensionWidth = 1000;
        int dimensionHeight = 800;
        String dateFormat = "dd/MM/yyyy hh:mm";

        PrintWriter experimentLog = null;
        try {
            experimentLog = new PrintWriter(new FileWriter(experimentFile, false));
        } catch (IOException ex) {
            System.err.println(ex.getMessage());
            System.exit(-1);
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);

        System.out.println("**************************************************");
        System.out.println("BomberCloudClient Tester");
        System.out.println("**************************************************");
        System.out.println("Parameters:");
        System.out.println("Scenario resolution: \t" + dimensionWidth + "x" + dimensionHeight);
        System.out.println("Avatar size: \t\t" + avatarWidth + "x" + avatarHeight);
        System.out.println("Block size: \t\t" + avatarWidth + "x" + avatarHeight);
        System.out.println("Bomb size: \t\t" + avatarWidth + "x" + avatarHeight);
        System.out.println("Bomber command: \t" + "off");
        System.out.println("Number of players: \t" + numberOfPlayers);
        System.out.println("Date format: \t\t" + dateFormat);
        System.out.println("Experiment time: \t" + ((experimentTime / 60) / 1000) + " minute(s)");
        System.out.println("Output file: \t\t" + experimentFile + "");
        System.out.println("**************************************************");

        System.out.println("Creating players...");
        Player[] players = new Player[numberOfPlayers];
        for (int i = 0; i < players.length; i++) {
            players[i] = new Player(i + 1, dimensionWidth, dimensionHeight, avatarWidth, avatarHeight, serverAddress, serverPort, experimentLog, simpleDateFormat);
            System.out.println("Player " + players[i].getId() + " was created");
        }

        long currentTime = System.currentTimeMillis();
        long endTime = currentTime + experimentTime;

        for (int i = 0; i < players.length; i++) {
            players[i].start();
            System.out.println("Player " + players[i].getId() + " was started");
        }

        long nextPrint = currentTime + (1000 * 60);
        int minutesRemaining = executionTime;
        while (currentTime < endTime) {
            currentTime = System.currentTimeMillis();
            if (currentTime >= nextPrint) {
                System.out.println("Remaining time of the experiment: " + (--minutesRemaining) + " minutes(s)");
                nextPrint = currentTime + (1000 * 60);
            }
        }

        for (int i = 0; i < players.length; i++) {
            players[i].deactivate();
            System.out.println("Player " + players[i].getId() + " was deactivated");
        }

        experimentLog.close();
        System.out.println("BomberCloudClient Tester was finished");
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (args == null || args.length != 3) {
            System.err.println("Invalid number of parameters");
            System.err.println("Example: java -jar bombercloud-client.jar 192.168.0.1 6789 C:/log.csv");
            System.exit(-1);
        }
        String address = args[0];
        int port = -1;
        try {
            port = Integer.parseInt(args[1]);
        } catch (Exception ex) {
            System.err.println(ex.getMessage());
            System.exit(-1);
        }
        String experimentFile = args[2];
        BomberCloudMain bomberCloudClient = new BomberCloudMain(address, port, experimentFile);
        bomberCloudClient.start();
    }
}
